package com.dmooo.ymx.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dmooo.ymx.R;
import com.dmooo.ymx.activity.NewsDetailActivity;
import com.dmooo.ymx.adapter.HealthAdapter;
import com.dmooo.ymx.adapter.XtAdapter;
import com.dmooo.ymx.bean.MessageCenterBean;
import com.dmooo.ymx.bean.Response;
import com.dmooo.ymx.common.T;
import com.dmooo.ymx.config.Constants;
import com.dmooo.ymx.https.HttpUtils;
import com.dmooo.ymx.https.onOKJsonHttpResponseHandler;
import com.dmooo.ymx.utils.MyGridView;
import com.dmooo.ymx.utils.RoundImageView2;
import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.RequestParams;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import cz.msebera.android.httpclient.Header;

/**
 * 健康
 */
public class HealthFragment extends Fragment {


    protected String token = "";
    private MyGridView gridView1;
    ViewPager viewpager;
//    private List<ImgBean> imgBeanList=new ArrayList<>();
    private XtAdapter xtAdapter;
    private List<MessageCenterBean.MessageCenterChildBean> xtlist = new ArrayList<>();
//    private List<MessageChildBean.MessageCenterChildBean> xtlist1= new ArrayList<>();
    private int xuetangPage = 1;
    private RecyclerView mRecyclerView;
    private HealthAdapter healthAdapter;

    public static HealthFragment newInstance(String tag){
        HealthFragment myFragment = new HealthFragment();
        Bundle bundle = new Bundle();
        bundle.putString("tag", tag);
        //传递参数
        myFragment.setArguments(bundle);
        return myFragment;
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.ac_headlth, null);
        //初始化控件
        gridView1=view.findViewById(R.id.gridView1);
        getArticleList();
        mRecyclerView = view.findViewById (R.id.home_recycler_view_layout);

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //获取参数
        Bundle arguments = getArguments();
    }

    //获取商学院文章列表
    private void getArticleList() {
        try {
            RequestParams requestParams = new RequestParams();
            requestParams.put("cat_id", 15);
            requestParams.put("p", xuetangPage);
            requestParams.put("per", 50);
            HttpUtils.post(Constants.MESSAGE_ARTICLE_GETARTICLELIST_URL, requestParams, new onOKJsonHttpResponseHandler<MessageCenterBean>(new TypeToken<Response<MessageCenterBean>>() {
            }) {
                @Override
                public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                    T.showShort(getActivity(),throwable.getMessage());
                }

                @Override
                public void onSuccess(int statusCode, Response<MessageCenterBean> datas) {
                    if (datas.isSuccess()) {
                        if (xuetangPage == 1) {
                            xtlist.clear();
                            xtlist.addAll(datas.getData().getList());
                        } else {
                            if (datas.getData().getList().size() <= 0) {
                                xuetangPage--;
                            }
                            xtlist.addAll(datas.getData().getList());
                        }
                        healthAdapter = new HealthAdapter(getActivity (),xtlist);
                        mRecyclerView.setLayoutManager (new LinearLayoutManager(getActivity (),LinearLayoutManager.VERTICAL,false));
                        mRecyclerView.setItemAnimator (new DefaultItemAnimator());
                        mRecyclerView.setAdapter (healthAdapter);
                        mRecyclerView.addItemDecoration (new DividerItemDecoration(getActivity (),DividerItemDecoration.VERTICAL));
//                        MyBaseAdapter mAdapter=new MyBaseAdapter();
//                        gridView1.setAdapter(mAdapter);
                        gridView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                                Bundle bundle = new Bundle();
                                bundle.putString("id", xtlist.get(i).getArticle_id());
                                bundle.putString("cat_id", xtlist.get(i).getCat_id());
                                Intent intent=new Intent(getActivity(), NewsDetailActivity.class);
                                intent.putExtras(bundle);
                                getActivity().startActivity(intent);
                            }
                        });
//                        gridView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//                            @Override
//                            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//                                Bundle bundle = new Bundle();
//                                bundle.putString("id", xtlist.get(i).getArticle_id());
//                                bundle.putString("cat_id", xtlist.get(i).getCat_id());
//                            }
//                        });
//
                    } else {
                    }
                }
            });
        } catch (Exception e) {
        }
    }


    class MyBaseAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return xtlist.size();
        }

        @Override
        public Object getItem(int position) {
            return xtlist.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = LayoutInflater.from(getActivity()).inflate(R.layout.ac_health_itemimg,null);
                holder=new ViewHolder();
                holder.imageView1=convertView.findViewById(R.id.imageView1);
                holder.content_tv=convertView.findViewById(R.id.content_tv);
                convertView.setTag(holder);
            }else {
                holder=(ViewHolder) convertView.getTag();
            }
            holder.content_tv.setText(Html.fromHtml(xtlist.get(position).getTitle()));
            Glide.with(getActivity()).load(Constants.APP_IP+xtlist.get(position).getImg()).into(holder.imageView1);
            return convertView;
        }
        class ViewHolder{
            TextView content_tv;
            RoundImageView2 imageView1;
        }
    }

}
